<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>66.加一</title>
</head>

<body>

</body>

<script>
    /**
     * @param {number[]} digits
     * @return {number[]}
     */
    var plusOne = function (digits) {
        digits[digits.length - 1] = digits[digits.length - 1] + 1;
        if (digits.includes(10)) {
            findTen(digits);
        }
        return digits;
    };
    function findTen(arr) {
        let index = arr.lastIndexOf(10)
        if (index != -1 && index != 0) {
            arr[index] = 0;
            arr[index - 1] += 1;
            console.log(index, arr)
            findTen(arr);
        }
        if (index == 0) {
            arr[index] = 0;
            arr.unshift(1);
        }
    }
    console.log(plusOne([9,9,9,9,9]))
</script>

</html>